﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace DBSourceToolsLib.CodeParser
{
    public class ParseBrace : ParseObject
    {
        public const int BRACKET_START = 1;
        public const int BRACKET_END = 2;
        public const int BRACE_START = 3;
        public const int BRACE_END = 4;
        public const int SINGLE_QUOTE = 5;
        public const int DOUBLE_QUOTE = 6;

        private static Hashtable m_KeywordHash;

        private int m_PartnerLineIdx = -1;
        private int m_PartnerWordIdx = -1;
        private bool m_IsStartBrace = false;

        public ParseBrace(char charValue, int iLineIdx, int iWordIdx)
            : base(ParseObject.UNKNOWN, ParseObject.BRACE, false, iLineIdx, iWordIdx)
        {
            m_Value = ParseBrace.computeValue(charValue);
            m_Data = charValue.ToString();
        }

        public static int computeValue(char charValue)
        {
            if (m_KeywordHash.ContainsKey(charValue.ToString()))
                return (int) m_KeywordHash[charValue.ToString()];
            return -1;
        }
        static ParseBrace()
        {
            m_KeywordHash = new Hashtable();
            m_KeywordHash.Add("{", BRACE_START);
            m_KeywordHash.Add("}", BRACE_END);
            m_KeywordHash.Add("(", BRACKET_START);
            m_KeywordHash.Add(")", BRACKET_END);
            m_KeywordHash.Add("\'", SINGLE_QUOTE);
            m_KeywordHash.Add("\"", DOUBLE_QUOTE);
        }

        public bool IsLegal
        {
            get { return m_Value > 0; }
        }

        public void SetPartner( int iPartLineIdx, int iPartWordIdx) {
            m_PartnerLineIdx = iPartLineIdx;
            m_PartnerWordIdx = iPartWordIdx;
        }
        public void SetIsStartBrace()
        {
            m_IsStartBrace = true;
        }

        public override string DumpToString()
        {
            string strPartner = String.Format("-[{0},{1}]", m_PartnerLineIdx, m_PartnerWordIdx); 
            return String.Format("{0} |{1}| {2} [{3},{4}]{5} {6}", GetTypeString(), m_Data, (IsResolved ? "1" : "0")
                , LineIdx, WordIdx,
                ((m_PartnerLineIdx > -1) ? strPartner : "")
                , (m_IsStartBrace ? "beg" : "end")
                );

        }
    }
}
